﻿// -----------------------------------------------------------------------
// <copyright file="StorageLocationProfileListData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db StorageLocationProfileList
    /// </summary>
    public class StorageLocationProfileListData
    {
        /// <summary>
        /// Get StorageLocationProfileList By Id
        /// </summary>
        /// <param name="storageLocationProfileListId">Param storageLocationProfileListId</param>
        /// <returns>Return StorageLocationProfileList</returns>
        public StorageLocationProfileListEN GetStorageLocationProfileListById(int storageLocationProfileListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                StorageLocationProfileListEN storageLocationProfileList = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneStorageLocationProfileListGetById, storageLocationProfileListId);
                while (reader.Read())
                {
                    storageLocationProfileList = new StorageLocationProfileListEN();

                    if (!reader.IsDBNull(0))
                    {
                        storageLocationProfileList.StorageLocationProfileListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        storageLocationProfileList.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        storageLocationProfileList.Name = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        storageLocationProfileList.Descriptions = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        storageLocationProfileList.Parent = reader.GetInt32(4);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return storageLocationProfileList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all StorageLocationProfileList
        /// </summary>
        /// <returns>Return list all StorageLocationProfileList</returns>
        public IList<StorageLocationProfileListEN> GetStorageLocationProfileLists()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<StorageLocationProfileListEN> lstStorageLocationProfileList = new List<StorageLocationProfileListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneStorageLocationProfileListGetAll);
                while (reader.Read())
                {
                    StorageLocationProfileListEN storageLocationProfileList = new StorageLocationProfileListEN();

                    if (!reader.IsDBNull(0))
                    {
                        storageLocationProfileList.StorageLocationProfileListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        storageLocationProfileList.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        storageLocationProfileList.Name = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        storageLocationProfileList.Descriptions = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        storageLocationProfileList.Parent = reader.GetInt32(4);
                    }
                    
                    lstStorageLocationProfileList.Add(storageLocationProfileList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstStorageLocationProfileList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db StorageLocationProfileList
        /// </summary>
        /// <param name="storageLocationProfileList">Paarm StorageLocationProfileList</param>
        /// <returns>Insert StorageLocationProfileList</returns>
        public int Insert(StorageLocationProfileListEN storageLocationProfileList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.Code, storageLocationProfileList.Code),
                                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.Name, storageLocationProfileList.Name),
                                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.Descriptions, storageLocationProfileList.Descriptions),
                                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.Parent, storageLocationProfileList.Parent)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneStorageLocationProfileListInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db StorageLocationProfileList
        /// </summary>
        /// <param name="storageLocationProfileList">Param StorageLocationProfileList</param>
        public void Update(StorageLocationProfileListEN storageLocationProfileList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.StorageLocationProfileListId, storageLocationProfileList.StorageLocationProfileListId),
                                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.Code, storageLocationProfileList.Code),
                                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.Name, storageLocationProfileList.Name),
                                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.Descriptions, storageLocationProfileList.Descriptions),
                                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.Parent, storageLocationProfileList.Parent)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneStorageLocationProfileListUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete StorageLocationProfileList by storageLocationProfileListId
        /// </summary>
        /// <param name="storageLocationProfileListId">param storageLocationProfileListId</param>
        public void Delete(int storageLocationProfileListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.StorageLocationProfileListId, storageLocationProfileListId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneStorageLocationProfileListDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get StorageLocationProfileList By parentId
        /// </summary>
        /// <param name="parentId">Param parentId</param>
        /// <param name="level">Param level</param>
        /// <returns>Return List StorageLocationProfileList</returns>
        public IList<StorageLocationProfileListEN> GetStorageLocationProfileListByParentId(int parentId, int level)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<StorageLocationProfileListEN> lstStorageLocationProfileList = new List<StorageLocationProfileListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneStorageLocationProfileByParentId, parentId, level);
                while (reader.Read())
                {
                    StorageLocationProfileListEN storageLocationProfileList = new StorageLocationProfileListEN();

                    if (!reader.IsDBNull(0))
                    {
                        storageLocationProfileList.StorageLocationProfileListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        storageLocationProfileList.Code = reader.GetString(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        storageLocationProfileList.Name = reader.GetString(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        storageLocationProfileList.Descriptions = reader.GetString(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        storageLocationProfileList.Parent = reader.GetInt32(4);
                    }

                    lstStorageLocationProfileList.Add(storageLocationProfileList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstStorageLocationProfileList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist code
        /// </summary>
        /// <param name="storageLocationProfileListId">Param storageLocationProfileListId</param>
        /// <param name="code">Param code</param>
        /// <returns>Return Exist</returns>
        public bool Exist(int storageLocationProfileListId, string code)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.StorageLocationProfileListId, storageLocationProfileListId),
                    new SqlParameter(Constants.SqlParamerterName.StorageLocationProfileList.Code, code)
                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneStorageLocationProfileExist, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}